﻿#region File and License Information
/*
<File>
	<License>
		Copyright © 2009 - 2017, Daniel Vaughan. All rights reserved.
		This file is part of Calcium (http://calciumsdk.net), 
		which is released under the MIT License.
		See file /Documentation/License.txt for details.
	</License>
	<CreationDate>2010-08-18 17:44:10Z</CreationDate>
</File>
*/
#endregion

using System;
using System.Windows;

using Outcoder.Services.MessageService;

namespace Outcoder.Services.Implementation
{
	public partial class MessageService : MessageServiceBase
	{
		public override MessageResult ShowCustomDialog(string message, string caption, 
			MessageButton messageButton, MessageImage messageImage, MessageImportance? importanceThreshold, 
			string details)
		{
			/* If the importance threshold has been specified 
			 * and it's less than the minimum level required (the filter level) 
			 * then we don't show the message. */
			if (importanceThreshold.HasValue && importanceThreshold.Value < MinumumImportance)
			{
				return MessageResult.OK;
			}

			if (!UISynchronizationContext.Instance.InvokeRequired)
			{	/* We are on the UI thread, and hence no need to invoke the call.*/
				var messageBoxResult = MessageBox.Show(message, caption,
					messageButton.TranslateToMessageBoxButton());
				return messageBoxResult.TranslateToMessageBoxResult();
			}

			MessageResult result = MessageResult.OK;
			UISynchronizationContext.Instance.InvokeAndBlockUntilCompletion(
				delegate
				{
					var messageBoxResult = MessageBox.Show(message, caption,
					messageButton.TranslateToMessageBoxButton());
					result = messageBoxResult.TranslateToMessageBoxResult();
				});

			return result;
		}

#if !WINDOWS_PHONE
		public override void AskQuestion<TResponse>(IQuestion<TResponse> question, Action<QuestionResult<TResponse>> resultHandler)
		{
			throw new NotImplementedException();
		}
#endif

		static MessageBoxButton ConvertToSilverlightButton(MessageButton messageButton)
		{
			switch (messageButton)
			{
				case MessageButton.OK:
					return MessageBoxButton.OK;
				case MessageButton.YesNo:
				case MessageButton.OKCancel:
				case MessageButton.YesNoCancel:
					return MessageBoxButton.OKCancel;
					default:
					throw new InvalidOperationException(
						"Unknown MessageButton " + messageButton);
			}
		}
	}
}
